/**
 * author: zhangwei
 * 角色新增、编辑
 */
var pageType = "add";

var main = {

    init: function() {
        property.setUserInfo();
        loadJob()
        pageType = localStorage.taskType;
        if(pageType == 'edit') {
            $('#submit').text('保存');
            // this.getDetailData();
            this.id = localStorage.taskId;
            loadData(this.id);
        }
        this.initTable();
    },
    initTable: function() {
        layui.use(['form','laydate','upload'], function() {
            var form = layui.form;
            laydate = layui.laydate;
            laydate.render({
                elem: '#startTime'
            });
            laydate.render({
                elem: '#endTime'
            });

            var upload = layui.upload;

            var uploadInst = upload.render({
                accept:'file',
                elem: '#test1' //绑定元素
                ,url: '/upload/' //上传接口
                ,done: function(res){
                    attach = res.data
                    $("#attach").val(attach);
                    successMsg("上传成功");
                }
                ,error: function(){
                    //请求异常回调
                    successMsg("上传失败");
                }
            });
            //监听提交
            form.on('submit(formSubmit)', function(data) {
                var url = "/task/save";
                var json = $("#taskForm").serialize();
                $.ajax({
                    type:"post",
                    data:json,
                    async:false,
                    url:url,
                    success:function(result) {
                        if (result.code == property.successCode) {
                            if(pageType == "edit"){
                                successMsg("修改成功");
                            }else{
                                successMsg("添加成功");
                            }
                            parent.$t.goback("/task/manage");
                        } else{
                            errorMsg(result.msg);
                        }
                    },
                    error:function(result) {
                        errorMsg("系统异常");
                    }
                });
                return false;
            });

            //监听重置
            $("#cancel").click(function () {
                    parent.$(".myRefresh").click();
                    layer.close(index);
                    return false;
            });

        });
    }
}
main.init();

/**
 * 加载表单数据
 * @param id 角色id
 */
function loadData(id) {
    this.type = "edit";
    layui.use('form', function(){
        var form = layui.form;
        var index = parent.layer.getFrameIndex(window.name);
        var json = {"id":id};
        //加载数据
        $.ajax({
            type:"get",
            data:json,
            async:false,
            url:"/task/getOne",
            success:function(result) {
                if (result.code == property.successCode) {
                    setFormData(result.data);
                    // form.render('select');
                } else {
                    errorMsg(result.msg);
                }
            },
            error:function(result) {
                errorMsg("系统异常");
            }
        });
    });
}

function loadJob() {
    $.ajax({
        type:"get",
        async:false,
        url:"/job/queryAllMyJob",
        success:function(result) {
            if (result.code == property.successCode) {
                data = result.data
                setJob(data)
            } else {
                errorMsg(result.msg);
            }
        },
        error:function(result) {
            errorMsg("系统异常");
        }
    });
}

function setJob(data) {
    htmlstr = ""
    for(i=0;i<data.length;i++){
        item = data[i]
        str = "<option value="+item['id']+">"+item['jobName']+"</option>"
        htmlstr = htmlstr+str
    }
    $("#jobId").html(htmlstr)
}

/**
 * 设置表单数据
 * @param data
 */
function setFormData(data) {
    data.startTime = formatSimpleDate(data.startTime)
    data.endTime = formatSimpleDate(data.endTime)
    property.setForm($("#taskForm"),data);
}



